package com.lch.designpattern.proxy.dynamicproxy.gpproxy;


import com.lch.designpattern.proxy.Person;

import java.lang.reflect.Method;

/**
 * 媒婆相当于动态代理增加，帮人找对象，用在aop里面就是帮特定类的方法增强一些功能 Created by Tom on 2019/3/10.
 */
// 很有用
public class GPemcee implements GPInvocationHandler {

    private Person person;

    public Object getInstance(Person person) throws Exception {
        this.person = person;
        Class<?> clazz = person.getClass();
        return GPProxy.newProxyInstance(new GPClassLoader(), clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(this.person, args);
        after();
        return obj;
    }

    // before增强
    private void before() {
        System.out.println("司仪开始支持婚礼");
        System.out.println("开始帮" + person.getName() + "与");
        System.out.println("=======");
    }

    // after增强
    private void after() {
        System.out.println("=======");
        System.out.println("结婚");
    }
}
